package com.iamteer.servlet;

import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import com.iamteer.entity.Category;
import com.iamteer.entity.Dish;


/**
 * 4. 菜品 Servlet 实现
 * @author Chuck
 *
 */
@WebServlet("/dish")
public class DishServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;

	private Dish prepareDish(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		Dish dish = null;
		try {
			DiskFileItemFactory factory = new DiskFileItemFactory();
			ServletFileUpload upload = new ServletFileUpload(factory);
			
			upload.setFileSizeMax(10 * 1024 * 1024);	// 单个文件最大 10M
			upload.setSizeMax(50 * 1024 * 1024);		// 总文件大小限制 50M
			upload.setHeaderEncoding("UTF-8");			// 中文编码
			
			if (ServletFileUpload.isMultipartContent(request)) {
				dish = new Dish();
				List<FileItem> list = upload.parseRequest(request);
				for (FileItem item : list) {
					if (item.isFormField()) {
						// 普通文本内容
						String name = item.getFieldName();
						String value = item.getString("UTF-8");
						BeanUtils.setProperty(dish, name, value);
					} else {
						// 上传内容
						String fieldName = item.getFieldName();
						// 只为调试方便，部署时需要修改
//						String path = request.getContextPath() + "/upload";
						String path = getServletContext().getRealPath("/upload");
						System.out.println(path);
						File f = new File(path);
						if (!f.exists()) {
							f.mkdir();
						}
						String name = item.getName();
						// 把图片路径保存到 dish.img 中
						BeanUtils.setProperty(dish, fieldName, "upload/" + name);
						File file = new File(path, name);
						item.write(file);
						item.delete();
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}		
		return dish;
	}
	public Object add(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		Dish dish = null;
		dish = prepareDish(request, response);
		if (dish != null) {
			dishService.add(dish);
			return request.getRequestDispatcher("/dish?method=list");
		} else {
			return "/error/error.jsp";
		}
	}
	
	public Object delete(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String id = request.getParameter("id");
		dishService.delete(Integer.parseInt(id));
		return request.getRequestDispatcher("/dish?method=list");
	}
	
	public Object update(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		Dish dish = null;
		dish = prepareDish(request, response);
		if (dish != null) {
			dishService.update(dish);
			return request.getRequestDispatcher("/dish?method=list");
		} else {
			return "/error/error.jsp";
		}
	}
	
	public Object show(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String id = request.getParameter("id");
		Dish dish = dishService.findById(Integer.parseInt(id));
		request.setAttribute("dish", dish);
		List<Category> list = categoryService.query();
		request.setAttribute("categoryList", list);
		return request.getRequestDispatcher("/sys/dish/updateDish.jsp");
	}
	
	public Object getCategory(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		List<Category> list = categoryService.query();
		request.setAttribute("categoryList", list);
		return request.getRequestDispatcher("/sys/dish/saveDish.jsp");
	}
	
	public Object list(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		List<Dish> dishs = dishService.query();
		request.setAttribute("dishList", dishs);
		List<Category> categories = categoryService.query();
		request.setAttribute("categoryList", categories);
		return request.getRequestDispatcher("/sys/dish/dishList.jsp");
	}
}
